import { connect } from 'react-redux'
import { Redirect, Route } from 'react-router-dom'
import { IGlobalState } from '../store/store-types'
import { E_ROUTE_PATH } from './route-path'

const mapState = (state: IGlobalState) => {
  return {
    isLoggedIn: state.auth.isLoggedIn
  }
}

type IStateProps = ReturnType<typeof mapState>

interface IPrivateRoute extends IStateProps {
  children: any
  path: string
}

function PrivateRouter(props: IPrivateRoute) {
  const { children, isLoggedIn, ...rest } = props
  return <Route {...rest} render={() => (isLoggedIn ? children : <Redirect to={E_ROUTE_PATH.LOGIN} />)} />
}

export default connect(mapState)(PrivateRouter)
